//
//  CYHeightAttestationVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/6/13.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYHeightAttestationVC.h"
#import "CYUser.h"
#import "CYHttpTool.h"
#import "CYLoginHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import <ZMCreditSDK/ALCreditService.h>
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import "CYDES3Util.h"
#import "CYYDVC.h"
#import "UIAlertView+Block.h"
#import "CYDXVC.h"
#import "CYLTVC.h"


@interface CYHeightAttestationVC ()
@property (weak, nonatomic) IBOutlet UIButton *zmfBtn;

@property (weak, nonatomic) IBOutlet UIButton *yysBtn;
@property (nonatomic, strong)NSString *zmfStr;
@property (nonatomic, strong)NSString *signstr;
@property (nonatomic, strong)NSString *pramstr;
@property (nonatomic, strong)NSString *submitparams;
@property (nonatomic, strong)UITextField *zmfdealCodeTF;
@property (weak, nonatomic) IBOutlet UILabel *yysLab;

@property (weak, nonatomic) IBOutlet UILabel *zmfLab;
@property (nonatomic, strong) NSString *mesStr;
@property (nonatomic, strong) NSString *accountStr;

@end

@implementation CYHeightAttestationVC

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"高级认证";
     
     [self setupNoti];
     [self loadmnoStatues];
     [self loadzmfstatues];
    // Do any additional setup after loading the view from its nib.
}

- (void)loadmnoStatues{
     CYUser *user = [CYLoginHttpTool userData].user;
     [CYHttpTool postWithURL:[CYURL shared].getMnoScore params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _yysLab.text = @"运营商    您已认证!";
               _yysBtn.hidden = YES;
          }
          
     } failure:^(NSError *error) {
          
     }];
}

- (IBAction)yunyingshangClick:(id)sender {
     
     [CYHttpTool postWithURL:[CYURL shared].getMnoAndroid params:@{} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               NSString *yysswich = jsoninfo.data;
                    if (yysswich&&yysswich .intValue == 0) {
                         //0 关闭，1打开
                         CYAlertView(@"暂未开放");
                    }else if (yysswich.intValue == 1){
                         UIAlertView *yysAlert = [[UIAlertView alloc]initWithTitle:@"请选择" message:@"请选择您手机号所属运营商" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"中国移动",@"中国联通", nil];
                         [yysAlert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
                              if (buttonIndex == 1) {
                                   //中国移动
                                   CYYDVC *ydvc = [[CYYDVC alloc]init];
                                   [self.navigationController pushViewController:ydvc animated:YES];
                              }
                              if (buttonIndex == 2) {
                                   //中国联通
                                   CYLTVC *ltvc = [[CYLTVC alloc]init];
                                   [self.navigationController pushViewController:ltvc animated:YES];
                              }
//                              if (buttonIndex == 3) {
//                                   //中国电信
//                                   CYDXVC *dxvc = [[CYDXVC alloc]init];
//                                   [self.navigationController pushViewController:dxvc animated:YES];
//                              }
                         }];
                    }
          }
          
     } failure:^(NSError *error) {
          
     }];
     
     
     
}
- (IBAction)zmfClick:(id)sender {
     [CYHttpTool postWithURL:[CYURL shared].getZhimafenSwitch params:@{} success:^(id json) {
               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
               if (jsoninfo.isSuccess) {
                    NSString *zmfswich = jsoninfo.data;
                    if (zmfswich.intValue == 0) {
                         //0 关闭，1打开
                         CYAlertView(@"暂未开放");
                    }else if (zmfswich.intValue == 1){
                         
                         [self loadparams];
                    }
               }
               
          } failure:^(NSError *error) {
               
          }];

}

-(void)loadparams{
     CYUser *user = [CYLoginHttpTool userData].user;
     UIAlertView *zmfalert = [[UIAlertView alloc]initWithTitle:@"请输入交易密码" message:_mesStr delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"授权", nil];
     [zmfalert setAlertViewStyle:UIAlertViewStylePlainTextInput];
     [zmfalert show];
     [zmfalert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
          if (buttonIndex == 1) {
               [MBProgressHUD showMessage:@"加载中..." toView:self.view];
               _zmfdealCodeTF = [zmfalert textFieldAtIndex:0];
               NSString *desstr = [CYDES3Util encrypt:_zmfdealCodeTF.text];
               [CYHttpTool postWithURL:[CYURL shared].generateZMXY params:@{@"userId":user.userId,@"token":user.token,@"dealCode":desstr} success:^(id json) {
                    
                    [MBProgressHUD hideHUDForView:self.view];
                    CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
                    CYAlertView(jsoninfo.message);
                    if (jsoninfo.isSuccess) {
                         [MBProgressHUD hideHUDForView:self.view];
                        
                         NSDictionary *dic = jsoninfo.data;
                         CYLog(@"dic = %@",dic);
                         _signstr = [dic objectForKey:@"sign"];
                         _pramstr = [dic objectForKey:@"params"];
                         [self loadZMF];
                    }
                    
               } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view];
               }];

               
          }
          
          
          
     }];
     
     
}
-(void)loadZMF{
     if (_pramstr&&_signstr) {
          NSString *appId = @"1000406";
          [[ALCreditService sharedService]queryUserAuthReq:appId sign:_signstr params:_pramstr extParams:nil selector:@selector(result:) target:self];
     }
}


- (void)result:(NSMutableDictionary*)dic{
     CYLog(@"dic = %@",dic);
     CYUser *user = [CYLoginHttpTool userData].user;
     _submitparams = [dic objectForKey:@"params"];
     [CYHttpTool postWithURL:[CYURL shared].getZmxyScore params:@{@"userId":user.userId,@"params":_submitparams} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _zmfStr = [NSString stringWithFormat:@"芝麻分:%@",jsoninfo.data];
               NSString *noticeStr = [NSString stringWithFormat:@"您已认证成功,当前芝麻分为:%@",_zmfStr];
               CYAlertView(noticeStr);
               
          }
          
     } failure:^(NSError *error) {
          
     }];
     
}


-(void)loadzmfstatues{
     CYUser *user  = [CYLoginHttpTool userData].user;
     [CYHttpTool postWithURL:[CYURL shared].getCreditZmxy params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _zmfStr = jsoninfo.data;
               NSString *messcount = jsoninfo.message;
               if (_zmfStr && messcount) {
                    _zmfLab.text = [NSString stringWithFormat:@"芝麻分  %@，额度为%@",_zmfStr,messcount];
                    _zmfBtn.hidden = YES;
                    return;
             }
          }else{
               [CYHttpTool postWithURL:[CYURL shared].getZhimafenMemo params:nil success:^(id json) {
                    CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
                    if (jsoninfo.success) {
                         NSDictionary *dic = jsoninfo.data;
                         if (dic.count) {
                              _mesStr = [dic objectForKey:@"zhimafenMemo"];
                              _accountStr = [dic objectForKey:@"zhimafenMoney"];
                              if (_accountStr) {
                                   _zmfLab.text = [NSString stringWithFormat:@"芝麻信用授权(自动审核额度，%@元/次)",_accountStr];
                              }
                              
                         }
                    }
                    
               } failure:^(NSError *error) {
                    CYLog(@"error");
                    
               }];

               
               
               
          }
     } failure:^(NSError *error) {
          
     }];
     
}

#pragma mark - 通知
- (void)setupNoti {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yysclick)name:CYYYSNotification object:nil];
     
}
-(void)yysclick{
     CYUser *user = [CYLoginHttpTool userData].user;
     [CYHttpTool postWithURL:[CYURL shared].getMnoScore params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               _yysLab.text = @"运营商    您已认证!";
               _yysBtn.hidden = YES;
          }
          
     } failure:^(NSError *error) {
          
     }];

     
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
